package org.example.tx.user;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.tx.acl.AclService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

@Service
@Slf4j
public class UserService {

    @Resource(name = "jdbcTemplate1")
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private AclService aclService;

    @Transactional(transactionManager = "transactionManager1")
    public void update(int id, boolean updateAcl) {
        int effected = jdbcTemplate.update(
                "update `user` set `name` = ? where `id` = ?",
                new Object[]{randomString(), id});
        log.info("effected: {}", effected);

        if (updateAcl) {
            aclService.update(1);
        }
    }

    private static String randomString() {
        return UUID.randomUUID().toString();
    }
}
